package fun.yaozun.study.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @Auther: yaozun
 * @Date: 2018/5/9 0009 17:29
 * @Description:
 */
@Configuration
@MapperScan(basePackages = StudyDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "studySqlSessionFactory")
public class StudyDataSourceConfig {
    // 精确到 master 目录，以便跟其他数据源隔离
    static final String PACKAGE = "fun.yaozun.study.mapper.study";
    static final String MAPPER_LOCATION = "classpath:fun/yaozun/study/mapper/study/*.xml";

    @Value("${study.datasource.url}")
    private String url;

    @Value("${study.datasource.username}")
    private String user;

    @Value("${study.datasource.password}")
    private String password;

    @Value("${study.datasource.driverClassName}")
    private String driverClass;

    @Bean(name = "studyDataSource")//获得一个数据源连接
    @Primary//多数据源一定要有一个主数据源
    public DataSource studyDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean(name = "studyTransactionManager")//配置事物管理
    @Primary
    public DataSourceTransactionManager studyTransactionManager() {
        return new DataSourceTransactionManager(studyDataSource());
    }

    @Bean(name = "studySqlSessionFactory")//配置数据库会话连接对象工厂
    @Primary
    public SqlSessionFactory studySqlSessionFactory(@Qualifier("studyDataSource") DataSource studyDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(studyDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(StudyDataSourceConfig.MAPPER_LOCATION));
        //加载mybatis配置文件 可以打印sql
        sessionFactory.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis-config.xml"));
        return sessionFactory.getObject();
    }

//    @Bean(name = "sqlSessionTemplateP2PRead")
//    public SqlSessionTemplate sqlSessionTemplateUserPrimaryRead(@Qualifier("sqlSessionFactoryP2PRead") SqlSessionFactory sqlSessionFactory) throws Exception {
//        return new SqlSessionTemplate(sqlSessionFactory);
//    }
}
